package com.zahb.qadx.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.zahb.qadx.R;
import com.zahb.qadx.model.Curriculum;
import com.zahb.qadx.model.RecommendModel;
import com.zahb.qadx.ui.activity.CurriculumDetailsActivity;
import com.zahb.qadx.util.ImageLoaderKt;

import java.util.List;

/**
 * Created by： huwei.
 * Data： 2020-10-21.
 * Time： 11:22.
 * Desc：课程推荐apdter
 */
public class RecommendAdapter extends RecyclerView.Adapter<RecommendAdapter.RecommendViewHodler> {
    private Context mContext;
    List<RecommendModel.RecomendData.RecommendList> recommendLists;
    public RecommendAdapter(Context mContext, List<RecommendModel.RecomendData.RecommendList> recommendLists) {
        this.mContext = mContext;
        this.recommendLists = recommendLists;
    }

    @NonNull
    @Override
    public RecommendViewHodler onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new RecommendViewHodler(LayoutInflater.from(mContext).inflate(R.layout.item_recommended_recommended, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull RecommendViewHodler holder, int position) {
       final RecommendViewHodler recommendViewHodler;
        holder.tv_name.setText(recommendLists.get(position).getName());
        holder.tv_subName.setText(recommendLists.get(position).getSubName());
        holder.tv_cateName.setText(recommendLists.get(position).getCateName());
        String coverImage = recommendLists.get(position).getCoverImage();
        ImageLoaderKt.loadImageSameCorners(holder.imageView,coverImage,5f);
        holder.tv_learn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Curriculum curriculum = new Curriculum();
                RecommendModel.RecomendData.RecommendList course = recommendLists.get(position);
                curriculum.setId(course.getId());
                curriculum.setSnapshotId(course.getId()+"");
                curriculum.setCourseUserId(course.getId()+"");
                curriculum.setName(course.getName());
                curriculum.setSubName(course.getSubName());
                curriculum.setCoverImage(course.getCoverImage());
                CurriculumDetailsActivity.actionStart(holder.itemView.getContext(), curriculum, CurriculumDetailsActivity.TYPE_FROM_HOME);
            }
        });
    }

    @Override
    public int getItemCount() {
        return recommendLists !=null ? recommendLists.size():0;
    }

    static class RecommendViewHodler extends RecyclerView.ViewHolder {
        private ImageView imageView;
        private TextView tv_name, tv_subName, tv_cateName, tv_learn;

        public RecommendViewHodler(@NonNull View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.img_icon);
            tv_name = itemView.findViewById(R.id.tv_name);
            tv_subName = itemView.findViewById(R.id.tv_subName);
            tv_cateName = itemView.findViewById(R.id.tv_catName);
            tv_learn = itemView.findViewById(R.id.tv_Learning);
        }
    }
}
